package web.servlets;

import domain.Category;
import domain.Product;

import persistence.Impl.TIMEtil;
import service.CatelogService;
import service.DialogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @projectName: petStore
 * @package: web.servlets
 * @className: ViewCategoryServlet
 * @author: suqianbei
 * @description: TODO
 * @date: 2021/10/31 10:39
 * @version: 1.0
 */
public class ViewCategoryServlet extends HttpServlet {
    private static final String VIEW_CATEGORY="/WEB-INF/jsp/catalog/Category.jsp";

    private String categoryId;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        categoryId=req.getParameter("categoryId");
        CatelogService service=new CatelogService();
        Category category=service.getCategory(categoryId);
        List<Product> productList=service.getProductListByCategory(categoryId);

        //diary 后期账号做好之后 选择判断存储账号还是IP
//        DialogService dialogService=new DialogService();
//        dialogService.insertNewDiary(req.getRemoteAddr(), TIMEtil.getTime(),categoryId,null,null,0);

        HttpSession session=req.getSession();
        session.setAttribute("category",category);
        session.setAttribute("productList",productList);
        session.setAttribute("symbol",0);
        req.getRequestDispatcher(VIEW_CATEGORY).forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }
}
